/*
**  common.c
**  Copyright 2011, Zane Sims
**
**  This file is part of brick-breaker-game.
**
**  brick-breaker-game is free software: you can redistribute it
**  and/or modify it under the terms of the GNU General Public
**  License as published by the Free Software Foundation, either
**  version 3 of the License, or (at your option) any later version.
**
**  brick-breaker-game is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with brick-breaker-game.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "common.h"

int APP_MODE = 0; /* Set this to either "EDITOR" or "GAME"*/
int BRICK_GRID_X1;
int BRICK_GRID_Y1;
int BRICK_GRID_X2;
int BRICK_GRID_Y2;

int bricks_remaining = 0;

ALLEGRO_BITMAP *explode_imgs[5];
ALLEGRO_BITMAP *item_imgs[5];
ALLEGRO_BITMAP *brick_imgs[40] = {NULL};
ALLEGRO_BITMAP *start_screen_img = NULL;
ALLEGRO_BITMAP *ball_img = NULL;
ALLEGRO_BITMAP *paddle_img[4] = {NULL, NULL, NULL, NULL};
ALLEGRO_BITMAP *top_wall_img = NULL;
ALLEGRO_BITMAP *left_wall_img = NULL;
ALLEGRO_BITMAP *right_wall_img = NULL;
Brick bricks[BRICK_GRID_W][BRICK_GRID_H];

void draw_bricks(int show_invis)
{
	int i = 0;
	int j = 0;

	for(i=0;i<BRICK_GRID_W;i++)
	{
		for(j=0;j<BRICK_GRID_H;j++)
		{
			if(bricks[i][j].type == BRICK_TYPE_INVISIBLE)
			{
				if(show_invis)
					al_draw_bitmap(brick_imgs[bricks[i][j].img_index], bricks[i][j].x, bricks[i][j].y, 0);
			}
			else if(bricks[i][j].type!=BRICK_TYPE_NONE)
				al_draw_bitmap(brick_imgs[bricks[i][j].img_index], bricks[i][j].x, bricks[i][j].y, 0);
			else if(bricks[i][j].explode_tick > 0)
			{
				al_draw_bitmap(explode_imgs[(bricks[i][j].explode_tick-1)/10], bricks[i][j].x-13, bricks[i][j].y-9, 0);
				bricks[i][j].explode_tick--;
			}
		}
	}
}

int load_level(const char* filename)
{
	FILE *fp = NULL;
	int i = 0;
	int j = 0;
	char header[10];

	bricks_remaining = 0;

	if(filename != NULL)
	{
		if(!(fp=fopen(filename, "rb")))
			return 0;

		fread(header, 1, 10, fp);

		if(strncmp(header, "BRICKLEVEL", 10) != 0)
			return 0;
	}

	for(j=0;j<BRICK_GRID_H;j++)
	{
		for(i=0;i<BRICK_GRID_W;i++)
		{
			if(filename == NULL)
			{
				bricks[i][j].img_index = 0;
				bricks[i][j].type = BRICK_TYPE_NONE;
			}
			else
			{
				fread(&(bricks[i][j].img_index), 1, 1, fp);
				fread(&(bricks[i][j].type), 1, 1, fp);
				bricks[i][j].explode_tick = 0;

				if(bricks[i][j].type!=BRICK_TYPE_NONE && bricks[i][j].type!=BRICK_TYPE_UNBREAKABLE)
					bricks_remaining++;
			}

			bricks[i][j].x = (i * BRICK_W) + BRICK_GRID_X1;
			bricks[i][j].y = (j * BRICK_H) + BRICK_GRID_Y1;
		}
	}

	if(filename != NULL)
		fclose(fp);

	return 1;
}

void load_images()
{
	int i = 0;
	char filename[256] = {'\0'};

	if(APP_MODE == GAME)
	{
		if(!(start_screen_img = al_load_bitmap("images/start_screen.png")))
			error_exit("Failed to load image: start_screen.png!");

		if(!(paddle_img[SMALL_PADDLE] = al_load_bitmap("images/paddle-small.png")))
			error_exit("Failed to load image: paddle-small.png!");

		if(!(paddle_img[MEDIUM_PADDLE] = al_load_bitmap("images/paddle-medium.png")))
			error_exit("Failed to load image: paddle-medium.png!");

		if(!(paddle_img[LARGE_PADDLE] = al_load_bitmap("images/paddle-large.png")))
			error_exit("Failed to load image: paddle-large.png!");

		if(!(paddle_img[XLARGE_PADDLE] = al_load_bitmap("images/paddle-xlarge.png")))
			error_exit("Failed to load image: paddle-xlarge.png!");

		if(!(top_wall_img = al_load_bitmap("images/wall-top.png")))
			error_exit("Failed to load image: wall-top.png!");

		if(!(left_wall_img = al_load_bitmap("images/wall-left.png")))
			error_exit("Failed to load image: wall-left.png!");

		if(!(right_wall_img = al_load_bitmap("images/wall-right.png")))
			error_exit("Failed to load image: wall-right.png!");

		if(!(ball_img = al_load_bitmap("images/ball.png")))
			error_exit("Failed to load image: ball.png!");

		for(i=0;i<4;i++)
		{
			sprintf(filename, "images/item_%d.png", i);

			if(!(item_imgs[i] = al_load_bitmap(filename)))
				error_exit("Failed to load item images!");
		}

		for(i=0;i<6;i++)
		{
			sprintf(filename, "images/explode_%d.png", i);

			if(!(explode_imgs[i] = al_load_bitmap(filename)))
				error_exit("Failed to load explode images!");
		}
	}

	for(i=0;i<40;i++)
	{
		sprintf(filename, "images/brick_%02d.png", i);

		if(!(brick_imgs[i] = al_load_bitmap(filename)))
			error_exit("Failed to load brick images!");
	}
}

void free_images()
{
	int i = 0;
	int j = 0;

	al_destroy_bitmap(start_screen_img);
	al_destroy_bitmap(paddle_img[SMALL_PADDLE]);
	al_destroy_bitmap(paddle_img[MEDIUM_PADDLE]);
	al_destroy_bitmap(paddle_img[LARGE_PADDLE]);
	al_destroy_bitmap(paddle_img[XLARGE_PADDLE]);
	al_destroy_bitmap(top_wall_img);
	al_destroy_bitmap(left_wall_img);
	al_destroy_bitmap(right_wall_img);
	al_destroy_bitmap(ball_img);

	for(i=0;i<40;i++)
	{
		al_destroy_bitmap(brick_imgs[i]);
	}

	for(i=0;i<4;i++)
	{
		al_destroy_bitmap(item_imgs[i]);
	}

	for(i=0;i<6;i++)
	{
		al_destroy_bitmap(explode_imgs[i]);
	}
}

int point_in_rect(int px, int py, int rx1, int ry1, int rx2, int ry2)
{
	if(px>=rx1 && px<=rx2 && py>=ry1 && py<=ry2)
		return 1;
	 
	return 0;
}

void error_exit(const char* error_str)
{
	fprintf(stderr, "%s\n", error_str);
	clean_up();
	exit(-1);
}
